package thread_pool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/23
 * Time:17:04
 * 脏读 反例
 **/
public class PoolDemo33 {
    static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                1,1,0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1000)
        );
        for (int i = 0; i < 2; i++) {
            MyTask t = new MyTask();
            executor.execute(t);
        }
    }

    static class MyTask extends Thread {

        //标识是否第一次访问
        static boolean first =  true;

        @Override
        public void run() {
            if (first){
                //第一次访问
                //存储用户信息
                threadLocal.set(this.getName()+" session info.");
                first = false;
            }
            String result = threadLocal.get();
            System.out.println(this.getName()+":"+result);
        }
    }
}
